package com.ylp.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylp.common.exception.ServiceException;
import com.ylp.common.response.PageResult;
import com.ylp.sys.domain.dto.SysLogDto;
import com.ylp.sys.domain.entity.SysLog;
import com.ylp.sys.mapper.SysLogMapper;
import com.ylp.sys.service.SysLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 系统日志表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2024-07-30
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {
    private final SysLogMapper sysLogMapper;

    public SysLogServiceImpl(SysLogMapper sysLogMapper) {
        this.sysLogMapper = sysLogMapper;
    }

    @Override
    public PageResult<SysLog> pageList(SysLogDto sysLog) {
        IPage<SysLog> page = null;
        // like 条件构造
        try {
            LambdaQueryWrapper<SysLog> wrapper = new LambdaQueryWrapper<>();
            LocalDateTime startTime = sysLog.getStartTime();
            LocalDateTime endTime = sysLog.getEndTime();
            if (startTime != null && endTime != null) {
                wrapper.between(SysLog::getCreateDate, startTime, endTime);
            } else if (startTime != null) {
                wrapper.ge(SysLog::getCreateDate, startTime);
            } else if (endTime != null) {
                wrapper.le(SysLog::getCreateDate, endTime);
            }
            wrapper.orderByDesc(SysLog::getCreateDate);
            // 分页构造
            page = new Page<>(sysLog.getCurrentPage(), sysLog.getPageSize());
            sysLogMapper.selectPage(page, wrapper);
        } catch (Exception e) {
            throw new ServiceException("查询失败");
        }
        return new PageResult<SysLog>(page.getRecords(), page.getTotal());
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        sysLogMapper.deleteBatchIds(ids);
    }
}
